package com.duk.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.duk.framwork.AjaxResponseEntity;
import com.duk.framwork.BaseController;
import com.duk.log.BizLogAspect;
import com.duk.model.Dept;
import com.duk.service.DeptService;
import com.duk.util.JacksonJsonUtil;
import com.duk.util.PageBean;

@Controller
@RequestMapping(value = "/dept")
public class DeptController extends BaseController
{
	@Resource
	private DeptService deptService;

	@RequestMapping("/list")
	public ModelAndView list()
	{
		ModelAndView mv = new ModelAndView("/dept/list");
		return mv;
	}

	@RequestMapping("/listData")
	@ResponseBody
	public Map<String, Object> listData(String deptName) throws IOException
	{

		int[] params = getEasyuiPageParams();
		int pageIndex = params[0];
		int pageSize = params[1];
		PageBean pb = deptService.getPageByCondition(pageIndex, pageSize, deptName);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", pb.getTotal());
		map.put("rows", pb.getData());
		return map;
	}

	/*
	 * List<Dept> list = deptService.list(); ModelAndView mv = new
	 * ModelAndView("/dept/list"); mv.addObject("list", list); return mv;
	 */

	// 增加页面
	@RequestMapping("/add")
	public ModelAndView add()
	{
		ModelAndView mv = new ModelAndView("/dept/add");
		return mv;
	}

	// 去增加
	@RequestMapping("/addSave")
	@ResponseBody
	public AjaxResponseEntity addSave(Dept dept)
	{
		try
		{
			deptService.add(dept);
			return new AjaxResponseEntity().setSuccess(true).setMessage("操作成功");
		} catch (Exception e)
		{
			return new AjaxResponseEntity().setSuccess(false).setMessage("操作失败");
		}

	}

	// 修改是获取数据deptID,deptName,digest
	@RequestMapping("/edit")
	public ModelAndView edit(Integer deptID)
	{
		Dept dept = deptService.getById(deptID);
		ModelAndView mv = new ModelAndView("/dept/edit");
		mv.addObject("dept", dept);
		return mv;
	}

	// 进行修改
	@RequestMapping("/editSave")
	@ResponseBody
	public AjaxResponseEntity editSave(Dept dept)
	{
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("id", dept.getDeptID());
		String str = JacksonJsonUtil.object2Json(params);
		BizLogAspect.setLogParams(str);
		try
		{
			deptService.update(dept);
			return new AjaxResponseEntity().setSuccess(true).setMessage("操作成功");
		} catch (Exception e)
		{
			e.printStackTrace();
			return new AjaxResponseEntity().setSuccess(false).setMessage("操作失败");
		}
	}

	@RequestMapping("/remove")
	@ResponseBody
	public AjaxResponseEntity remove(@RequestParam("ids") Integer[] deptID)
	{
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("ids", deptID);
		String str = JacksonJsonUtil.object2Json(params);
		BizLogAspect.setLogParams(str);
		try
		{
			deptService.deleteByPKs(deptID);
			return new AjaxResponseEntity().setSuccess(true).setMessage("删除成功");
		} catch (Exception e)
		{
			e.printStackTrace();
			return new AjaxResponseEntity().setSuccess(false).setMessage("删除失败");
		}
	}

}
